<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/cookie.js"></script>
    <script src="../js/ajax.js"></script>
    <script src="../js/interface.js"></script>
    <style>
        #createCode {
            user-select: none;
        }

        .right {
            color: green;
            font-size: 12px;
        }

        .error {
            color: red;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <!-- -->
    <div class="box">
        <p>
            <label for="">用户名:</label>
            <input type="text" class="user">
            <span class="user_span"></span>
        </p>
        <p>
            <label for="">密&emsp;码:</label>
            <input type="text" class="pwd">
            <span class="pwd_span"></span>
        </p>
        <p>
            <input class="btn" type="submit" value="登录">
        </p>
    </div>
</body>
<script>
    var userInp = document.getElementsByClassName("user")[0];
    var userSpan = document.getElementsByClassName("user_span")[0];

    var pwdInp = document.getElementsByClassName("pwd")[0];
    var pwdSpan = document.getElementsByClassName("pwd_span")[0];

    var btn = document.getElementsByClassName("btn")[0];

    // 用户输入的用户名和密码   => 都传递到后端
    btn.onclick = function () {
        var user = userInp.value; // 'a123123'
        var pwd = pwdInp.value;

        if (user && pwd) {
            // var xhr = new XMLHttpRequest();

            // xhr.open("post", "../php/login.php", true);

            // xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            // xhr.send(`user=${user}&pwd=${pwd}`);

            // xhr.onreadystatechange = function () {
            //     if (xhr.readyState == 4 && xhr.status == 200) {
            //         var result = xhr.responseText;
            //         result = JSON.parse(result);
            //         console.log(result);

            //         var { status, message } = result;

            //         if (status) {
            //             setCookie("lgc", user, 31);
            //             location.href = "./index.html";
            //         } else {
            //             alert(message);
            //         }
            //     }
            // }

            login({ user, pwd }).then(result => {
                var { status, message } = result;
                if (status) {
                    setCookie("lgc", user, 31);
                    location.href = "./index.html";
                } else {
                    alert(message);
                }
            }).catch(err => {
                throw err;
            })



        }




    }





</script>

</html>